<?php 
require_once 'Command.php';
require_once 'database/GrosseryList.php';
require_once 'database/DatabaseConnection.php';

class GetGrosseryListCommand implements Command {
	
	private $id;
	private $grosseryList;
	
	public function __construct($id){
		$this->id = $id;
		$this->grosseryList = new GrosseryList();
	}
	
	public function execute(){
		$string = "<grocerylist>";
		DatabaseConnection::connect();
		$it = $this->grosseryList->getGrosseryListInfo($this->id)->getIterator();
		DatabaseConnection::disconnect();
		if($it->valid()){
			$arr = $it->current();
			$string .= "<id>".$arr['id']."</id>";
			$string .= "<name>".$arr['name']."</name>";
		}
		DatabaseConnection::connect();
		$arrayObj = $this->grosseryList->readGrosseryList($this->id);
		DatabaseConnection::disconnect();
		$it = $arrayObj->getIterator();
		$string .= 	"<products>";
		while($it->valid()){
			$arr = $it->current();
			$string .= 	"<product>";
			$string .= 		"<id>".$arr['productid']."</id>";
			$string .= 		"<name>".$arr['productname']."</name>";
			$string .= 		"<price>".$arr['price']."</price>";
			$string .= 		"<amount>".$arr['amount']."</amount>";
			$string .=		"<tag>".$arr['tag']."</tag>";
			$string .= 	"</product>";
			$it->next();
		}
		$string .= 	"</products>";
		$string .= "</grocerylist>";
		return $string;
	}
}
?>